package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.dto.MillionaireWithdrawDto;
import cn.com.duiba.millionaire.center.api.enums.WithdrawStatusEnum;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireWithdrawDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireWithdrawEntity;
import cn.com.duiba.millionaire.center.biz.service.MillionaireWithdrawService;
import cn.com.duiba.wolf.utils.BeanUtils;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by xiaoxuda on 2018/2/1.
 */
@Service
public class MillionaireWithdrawServiceImpl implements MillionaireWithdrawService {
    @Autowired
    private MillionaireWithdrawDao millionaireWithdrawDao;

    private Trans2Dto trans2Dto = new Trans2Dto();
    private Trans2Entity trans2Entity = new Trans2Entity();

    @Override
    public long insert(MillionaireWithdrawDto entity) {
        return millionaireWithdrawDao.insert(trans2Entity.apply(entity));
    }

    @Override
    public MillionaireWithdrawDto findById(long id) {
        return trans2Dto.apply(millionaireWithdrawDao.findById(id));
    }

    @Override
    public List<MillionaireWithdrawDto> batchFindByConsumerId(long consumerId) {
        return Lists.transform(millionaireWithdrawDao.batchFindByConsumerId(consumerId), trans2Dto);
    }

    @Override
    public Long countWithdrawAmount(Long consumerId) {
        return millionaireWithdrawDao.countWithdrawAmount(consumerId);
    }

    @Override
    public boolean update2UserPayed(long id) {
        return millionaireWithdrawDao.update2UserPayed(id);
    }

    @Override
    public boolean update2DevPayed(long id) {
        return millionaireWithdrawDao.update2DevPayed(id);
    }

    @Override
    public boolean update2Success(long id) {
        return millionaireWithdrawDao.update2Success(id);
    }

    @Override
    public boolean update2Fail(long id, String failMsg) {
        return millionaireWithdrawDao.update2Fail(id, failMsg);
    }

    class Trans2Entity implements Function<MillionaireWithdrawDto, MillionaireWithdrawEntity>{
        @Override
        public MillionaireWithdrawEntity apply(MillionaireWithdrawDto millionaireWithdraw) {
            if(millionaireWithdraw == null){
                return null;
            }
            MillionaireWithdrawEntity entity = BeanUtils.copy(millionaireWithdraw, MillionaireWithdrawEntity.class);
            if(millionaireWithdraw.getWithdrawStatus() != null) {
                entity.setWithdrawStatus(millionaireWithdraw.getWithdrawStatus().getCode());
            }
            return entity;
        }
    }

    class Trans2Dto implements Function<MillionaireWithdrawEntity, MillionaireWithdrawDto> {
        @Override
        public MillionaireWithdrawDto apply(MillionaireWithdrawEntity millionaireWithdraw) {
            if(millionaireWithdraw == null){
                return null;
            }
            MillionaireWithdrawDto dto = BeanUtils.copy(millionaireWithdraw, MillionaireWithdrawDto.class);
            dto.setWithdrawStatus(WithdrawStatusEnum.getByCode(millionaireWithdraw.getWithdrawStatus()));
            return dto;
        }
    }
}
